<template>
  <div class="image-view" @click="onClick" :style="{height}">
    <img
      :class="round ? 'round image' : 'image'"
      :style="{height}"
      :src="src"
      :lazy-load="lazyLoad"
      :mode="mode"
      @load="onLoad"
      @error="onError"
      v-show="!isLoading && !error"
    >
    <img
      src="https://www.youbaobao.xyz/book/img/loading2.ae9e5924.jpeg"
      :class="round ? 'round image' : 'image'"
      :style="{height}"
      :lazy-load="lazyLoad"
      :mode="mode"
      v-show="isLoading || error"
    >
  </div>
</template>

<script>
export default {
  name: 'index',
  props: {
    src: {
      type: String,
      default: ''
    },
    mode: {
      type: String,
      default: 'widthFix'
    },
    lazyLoad: {
      type: Boolean,
      default: true
    },
    round: {
      type: Boolean,
      default: false
    },
    height: {
      type: String,
      default: 'auto'
    }
  },
  data () {
    return {
      isLoading: true,
      error: false
    }
  },
  watch: {
    src (newValue, preValue) {
      if (newValue && newValue.length > 0 && newValue !== preValue) {
        this.$nextTick(() => {
          this.isLoading = true
          this.error = false
        })
      }
    }
  },
  methods: {
    onClick () {
      this.$emit('onClick')
    },
    onLoad () {
      this.isLoading = false
      this.error = false
    },
    onError () {
      console.log('error')
      this.error = true
      this.isLoading = false
    }
  }
}
</script>

<style lang="stylus" scoped>
.image-view
  width 100%

  .image
    width 100%

    &.round
      border-radius 50%
</style>
